package com.example.cyclops;

import java.util.ArrayList;

import org.json.JSONArray;

import com.example.cyclops.DataUtils.PlayerControl;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MythDiscovery extends Activity {
	ArrayList<String> CameraDevices = new ArrayList<String>();
	ArrayList<PlayerControl> control = new ArrayList<PlayerControl>();;
	String ip;
	int userid;
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {  //获取 back键
			//confirm
			finish();
    	}
		return true;
	}
	/*
				
	*/
	private void RefreshListDevices(){
		final Spinner spinner = (Spinner)findViewById(R.id.spinnerCamera);
		spinner.setSelection(0);
		CameraDevices.clear();
		control = new DataUtils(Global.remoteip,Global.remoteport).DeepList(ip);
		if(control.size() > 0){
			for(PlayerControl t:control){
				CameraDevices.add(t.getHostname());
			}
		}else{
			RefreshDevices();
		}
	}
	
	private void RefreshDevices(){
		final Spinner spinner = (Spinner)findViewById(R.id.spinnerCamera);
		spinner.setSelection(0);
		CameraDevices.clear();
		control = new DataUtils(Global.remoteip,Global.remoteport).Search(ip);
		for(PlayerControl t:control){
			CameraDevices.add(t.getHostname());
		}
		
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.mythdiscovery);

        Bundle bundle = this.getIntent().getExtras();
		ip = bundle.getString("ip");
		userid = bundle.getInt("userid");
		RefreshListDevices();
		final Spinner spinner = (Spinner)findViewById(R.id.spinnerCamera);
		final Button btnModify = (Button)findViewById(R.id.buttonModify);
		final Button btnSubmit = (Button)findViewById(R.id.buttonSubmit);
		final Button btnSearch = (Button)findViewById(R.id.buttonSearch);
/*
		final EditText mykeyname = (EditText)findViewById(R.id.editName);
		final EditText myusername = (EditText)findViewById(R.id.editUsername);
		final EditText mypassword = (EditText)findViewById(R.id.editPassword);
	*/	
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_single_choice,
				CameraDevices);
		spinner.setAdapter(adapter);

		final Button btnback = (Button)findViewById(R.id.login_reback_btn);
		btnback.setOnClickListener(new OnClickListener(){	
				@Override
				public void onClick(View arg0) {
					finish();
				}
		}                                                                                                                                   );
		
        //添加事件Spinner事件监听    
        spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
        btnSearch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//search list
				RefreshDevices();
				
			}
		});
        
        btnModify.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//on submit
				//new DataUtils(Global.remoteip, Global.remoteport).Update(ip, control, userid);
				int index = spinner.getSelectedItemPosition();


				final EditText mykeyname = (EditText)findViewById(R.id.editName);
				final EditText myusername = (EditText)findViewById(R.id.editUsername);
				final EditText mypassword = (EditText)findViewById(R.id.editPassword);
				
				String keyname = mykeyname.getText().toString();
				String username = myusername.getText().toString();
				String password = mypassword.getText().toString();
				if(index < control.size() && index >= 0){
					PlayerControl singleControl = control.get(index);
					singleControl.SetParams(keyname, username, password);
					Toast.makeText(
						MythDiscovery.this,
	                   "修改成功", Toast.LENGTH_SHORT).show();
				}
			}
		});
        
        btnSubmit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//on submit
				Boolean ret = new DataUtils(Global.remoteip, Global.remoteport).Update(ip, control, userid);
				if(ret){
					Toast.makeText(
						MythDiscovery.this,
	                   "修改成功", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(
						MythDiscovery.this,
		                "修改失败", Toast.LENGTH_SHORT).show();
				}
			}
		});
          
	}

    //使用数组形式操作  
    class SpinnerSelectedListener implements OnItemSelectedListener{  
		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			if(arg2 < 0)
				return;
			final EditText mykeyname = (EditText)findViewById(R.id.editName);
			final EditText myusername = (EditText)findViewById(R.id.editUsername);
			final EditText mypassword = (EditText)findViewById(R.id.editPassword);
			
			if(arg2 < control.size() && arg2 >= 0){
				PlayerControl singleControl = control.get(arg2);
				mykeyname.setText(singleControl.getName());
				myusername.setText(singleControl.getUsername());
				mypassword.setText(singleControl.getPassword());
			}
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
		}  
    }
}
